#!/bin/sh /etc/rc.common
#
# Copyright (c) 2014, The Linux Foundation. All rights reserved.
#
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#
#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

START=
SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1

start () {
	config_load macsec

	local enable
	config_get_bool enable global enable 0
	[ "${enable}" -gt 0 ] || return

	config_get ifname global ifname

	config_get eapol_version global eapol_version 3
	eapol_version="eapol_version=$eapol_version"

	config_get ap_scan global ap_scan 0
	ap_scan="ap_scan=$ap_scan"

	config_get key_mgmt global key_mgmt
	key_mgmt="key_mgmt=$key_mgmt"

	config_get eap global eap
	eap="eap=$eap"

	config_get identity global identity
	identity="identity=\"$identity\""

	config_get password global password
	password="password=\"$password\""

	config_get eapol_flags global eapol_flags 0
	eapol_flags="eapol_flags=$eapol_flags"

	config_get macsec_policy global macsec_policy 1
	macsec_policy="macsec_policy=$macsec_policy"

	rm -rf /var/run/wpa_supplicant-macsec-$ifname*

	SERVICE_PID_FILE=/var/run/wpa_supplicant-macsec-$ifname.pid
	cat > /var/run/wpa_supplicant-macsec-$ifname.conf <<EOF

ctrl_interface=/var/run/wpa_supplicant-macsec-$ifname
$eapol_version
$ap_scan
network={
	$key_mgmt
	$eap
	$identity
	$password
	$eapol_flags
	$macsec_policy
}
EOF
	service_start /usr/sbin/wpa_supplicant -D macsec_qca -i "$ifname" -c /var/run/wpa_supplicant-macsec-$ifname.conf
}

stop() {
	config_load macsec

	config_get ifname global ifname

	SERVICE_PID_FILE=/var/run/wpa_supplicant-macsec-$ifname.pid
	service_stop /usr/sbin/wpa_supplicant
}
